<template>
  <Layer :layer="layer" @confirm="submit" ref="layerDom">
    <el-form :model="form" :rules="rules" ref="ruleForm" label-width="120px" style="margin-right: 30px">
      <el-form-item label="接口介绍：" prop="name">
        <el-input v-model="form.description" placeholder="接口介绍"></el-input>
      </el-form-item>
      <el-form-item label="服务选择" prop="select">
        <el-cascader v-model="form.serviceController" style="width: 100%;" @change="handleCascaderChange"
                     :options="cascaderData"
                     :props="{ value: 'controller', label: 'controllerDescription' }"></el-cascader>
      </el-form-item>
      <el-form-item label="接口地址" prop="select">
        <el-input v-model="form.path" placeholder="接口地址"></el-input>
      </el-form-item>
      <el-form-item label="请求类型：" prop="number">
        <el-select style="width: 100%;" v-model="form.type" placeholder="请选择" clearable>
          <el-option v-for="item in selectData" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否自动更新" prop="select">
        <el-switch v-model="form.isUpdate" active-color="#13ce66" inactive-color="#ff4949"
                   @click="handleUpdateClick(form.isUpdate)"></el-switch>
      </el-form-item>
      <el-form-item label="状态(是否启用)" prop="select">
        <el-switch v-model="form.status" active-color="#13ce66" inactive-color="#ff4949"
                   @click="handleStatusClick(form.isUpdate)"></el-switch>
      </el-form-item>
    </el-form>
  </Layer>
</template>

<script lang="ts" setup>
import type {LayerType} from "@/components/layer/index.vue";
import type {Ref} from "vue";
import type {ElFormItemContext} from "element-plus/lib/el-form/src/token";
import {ref} from "vue";
import {add, update, getService} from "@/api/system/resource";
import Layer from "@/components/layer/index.vue";
import {CascaderValue, ElMessage} from "element-plus";

const props = defineProps({
  layer: {
    type: Object,
    default() {
      return {
        show: false,
        title: "",
        showButton: true,
      };
    },
  },
});
const cascaderData = ref([])
const selectData = [
  {
    value: 1,
    label: "Get",
  },
  {
    value: 2,
    label: "Post",
  },
  {
    value: 3,
    label: "Put",
  },
  {
    value: 4,
    label: "Delete",
  }
];
const ruleForm: Ref<ElFormItemContext | null> = ref(null);
const layerDom: Ref<LayerType | null> = ref(null);
const emits = defineEmits(["getTableData"]);
let form = ref({
  type: "",
  path: "",
  description: "",
  service: '',
  isUpdate: false,
  status: true,
  serviceController: ["", ""],
  controller: ''
});
const rules = {};
init();

function init() {
  // 用于判断新增还是编辑功能
  if (props.layer.row) {
    form.value = JSON.parse(JSON.stringify(props.layer.row)); // 数量量少的直接使用这个转

    let serviceController: any = [form.value.service, props.layer.row.controller];
    form.value.serviceController = serviceController;

  } else {
  }
}

const handleCascaderChange = (value: CascaderValue) => {
  let arr = value as Array<string>;
  // 筛选出正确的父 service 值
  let controller = arr[arr.length - 2]
  let father: any = cascaderData.value.filter((item: any) => {
    return item.controller.indexOf(controller) > -1;
  });
  father = father[father.length - 1]

  const service = father.service;
  //设置提取 path 值
  form.value.path = "/" + service + "/" + arr[arr.length - 1];
}

const submit = () => {
  ruleForm.value.validate((valid: boolean) => {
    if (valid) {
      let params = form.value;
      if (props.layer.row) {
        updateForm(params);
      } else {
        addForm(params);
      }
    } else {
      return false;
    }
  });
};
// 新增提交事件
const addForm = (params: object) => {
  add(params).then((res) => {
    ElMessage({
      type: "success",
      message: "新增成功",
    });
    props.layer.show = false;
    emits("getTableData", true);
  });
};

// 编辑提交事件
const updateForm = (params: object) => {
  update(params).then((res) => {
    ElMessage({
      type: "success",
      message: "编辑成功",
    });
    emits("getTableData", false);
  });
};

const handleUpdateClick = (status: boolean) => {
  form.value.isUpdate = status ? true : false;
};
const handleStatusClick = (status: boolean) => {
  form.value.status = status ? true : false;
};

getService().then((res) => {
  cascaderData.value = res.data;
  // 计算出正确的父 serviceController 值
  let controller = form.value.serviceController[form.value.serviceController.length - 2]
  let father: any = cascaderData.value.filter((item: any) => {
    return item.service.indexOf(controller) > -1;
  });
  form.value.serviceController = [father[father.length - 1].controller, form.value.controller]
});
</script>

<style lang="scss" scoped></style>